home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_01 / chap10.txt < prev    next >
Text File  |  1992-01-18  |  18KB  |  412 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.                                                        Chapter 10
  7.                                                 VIRTUAL FUNCTIONS
  8.  
  9. Once again we are into a completely new topic with terminology
  10. which will be new to you.  If you are new to object oriented
  11. programming, you should follow along in this chapter very carefully
  12. because every attempt has been made to define every detail of this
  13. new and somewhat intimidating topic.  However, if you are well
  14. versed in object oriented programming, simply learning to use C++,
  15. you may wish to skip the first four programs in this chapter and
  16. go directly to the example program named VIRTUAL5.CPP and continue
  17. from there to the end of the chapter.
  18.  
  19. One term which must be defined is polymorphism, a rather large word
  20. that simply means similar when used in the context of object
  21. oriented programming.  Objects are polymorphic if they have some
  22. similarities but are still somewhat different.  We will see how it
  23. is used in the context of object oriented programming as we proceed
  24. through this chapter.
  25.  
  26. We have already studied operator overloading and function
  27. overloading in this tutorial, and they are a subtle form of
  28. polymorphism since in both cases, a single entity is used to refer
  29. to two or more things.  The use of virtual functions can be a great
  30. aid in programming some kinds of projects as you will see in these
  31. two chapters.
  32.  
  33.  
  34. A SIMPLE PROGRAM WITH INHERITANCE
  35. _________________________________________________________________
  36.  
  37. Examine the example program named VIRTUAL1.CPP   ================
  38. for the basic program outline we will use for      VIRTUAL1.CPP
  39. all discussion in this chapter.  Since this      ================
  40. program has nothing to do with virtual
  41. functions, the name may be somewhat misleading.
  42. It is named VIRTUAL1.CPP because it is part of a series of programs
  43. intended to illustrate the use of virtual functions. The last
  44. program in this chapter will illustrate the proper use of virtual
  45. functions.
  46.  
  47. The first program is very simple and you will recognize it as being
  48. somewhat similar to the programs studied in the last chapter except
  49. that this program is greatly simplified in order to effectively
  50. instruct you in the use of a virtual function.  You will notice
  51. that many of the methods from the last chapter have been completely
  52. dropped from this example for simplicity, and a new method has been
  53. added to the parent class, the method named message() in line 8.
  54.  
  55. Throughout this chapter we will be studying the operation of the
  56. method named message() in the base class and the derived classes.
  57.  
  58.                                                         Page 10-1
  59.  
  60.                                    Chapter 10 - Virtual Functions
  61.  
  62. For that reason, there is a method named message() in the car class
  63. as well as in the new class named boat in lines 27 through 32.
  64.  
  65. You will also notice that there is a lack of a method named
  66. message() in the truck class.  This has been done on purpose to
  67. illustrate the use of the virtual method, or if you prefer, you can
  68. refer to it as a virtual function.  You will recall that the method
  69. named message() from the base class is available in the truck class
  70. because the method from the base class is inherited with the
  71. keyword public included in line 19.  You will also notice that the
  72. use of the keyword public in lines 12 and 27 actually do nothing
  73. because the only method available in the base class is also
  74. available in the derived classes.  There are no methods actually
  75. inherited.  Leaving the keyword in the header poses no problem
  76. however, so it will be left there for your study.
  77.  
  78. The method named message() in the base class and in the derived
  79. classes has been kept very simple on purpose.  Once again, we are
  80. interested in the technique of the virtual method rather than a
  81. long complicated example.
  82.  
  83. The main program is as simple as the classes, one object of each
  84. of the classes is declared in lines 37 through 40 and the method
  85. named message() is called once for each object.  The result of
  86. executing the program indicates that the method for each is called
  87. except for the object named semi, which has no method named
  88. message().  As discussed in the last chapter, the method named
  89. message() from the parent class is called and the data output to
  90. the monitor indicates that this did happen since it displays
  91. "Vehicle message" for the object named semi.
  92.  
  93. The data for the objects is of no concern in this chapter so all
  94. data is allowed to default to private type and none is inherited
  95. into the derived classes.  Some of the data is left in the example
  96. program simply to make the classes look like classes.  Based on
  97. your experience with C++ by now, you realize that the data could
  98. be removed since it is not used.
  99.  
  100. After you understand this program, compile and execute it to see
  101. if your compiler gives the same result of execution.
  102.  
  103.  
  104. ADDING THE KEYWORD VIRTUAL
  105. _________________________________________________________________
  106.  
  107. As you examine the next example program named    ================
  108. VIRTUAL2.CPP, you will notice that there is one    VIRTUAL2.CPP
  109. small change in line 8.  The keyword virtual has ================
  110. been added to the declaration of the method
  111. named message() in the parent class.
  112.  
  113. It may be a bit of a disappointment to you to learn that this
  114. program operates no differently than the last example program.
  115. This is because we are using objects directly and virtual methods
  116.  
  117.                                                         Page 10-2
  118.  
  119.                                    Chapter 10 - Virtual Functions
  120.  
  121. have nothing to do with objects, only with pointers to objects as
  122. we will see soon.  There is an additional comment in line 46
  123. illustrating that since all four objects are of different classes,
  124. it is impossible to assign any object to any other object in this
  125. program.  We will soon see that some pointer assignments are
  126. permitted between objects.
  127.  
  128. After you are sure that the fact that they are virtual functions,
  129. or methods, has nothing to do with the objects as they are
  130. instantiated, compile and execute this example program to see if
  131. your compiler results in the same output as that listed.
  132.  
  133.  
  134. USING OBJECT POINTERS
  135. _________________________________________________________________
  136.  
  137. Examine the example program named VIRTUAL3.CPP   ================
  138. and you will find a repeat of the first program    VIRTUAL3.CPP
  139. but with a different main program.               ================
  140.  
  141. In this program the keyword virtual has been
  142. removed from the method declaration in the parent class in line 8,
  143. and the main program declares pointers to the objects rather than
  144. declaring the objects themselves in lines 37 through 40.  Since we
  145. only declared pointers to the objects we find it necessary to
  146. allocate the objects before using them by using the new operator
  147. in lines 42 through 49.  Upon running the program, we find that
  148. even though we are using pointers to the objects we have done
  149. nothing different than what we did in the first program.  Upon
  150. execution, we find that the program operates in exactly the same
  151. manner as the first example program in this chapter.  This should
  152. not be surprising because a pointer to a method can be used to
  153. operate on an object in the same manner as an object can be
  154. manipulated.
  155.  
  156. Be sure to compile and execute this program before continuing on
  157. to the next example program.  The observant student will notice
  158. that we failed to deallocate the objects prior to terminating the
  159. program.  As always, in such a simple program, it doesn't matter
  160. because the heap will be cleaned up automatically when we return
  161. to the operating system.
  162.  
  163.  
  164. A POINTER AND A VIRTUAL FUNCTION
  165. _________________________________________________________________
  166.  
  167. The example program named VIRTUAL4.CPP is        ================
  168. identical to the last program except for the       VIRTUAL4.CPP
  169. addition of the keyword virtual to line 8 once   ================
  170. again.
  171.  
  172. I hope you are not terribly disappointed to find that this program,
  173. including the keyword vir